/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.TinNhan_DTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class TinNhan_DAO {
    
    public static ArrayList<TinNhan_DTO> GetALL()
    {
        ArrayList<TinNhan_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from TinNhan" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                TinNhan_DTO TinNhan = new TinNhan_DTO();
                TinNhan.setID(rs.getInt("ID"));
                TinNhan.setTK_Gui(rs.getString("TK_Gui"));
                TinNhan.setTK_Nhan(rs.getString("TK_Nhan"));
                TinNhan.setTinNhan(rs.getString("TinNhan"));
                TinNhan.setActive(rs.getBoolean("Active"));
                TinNhan.setTime(rs.getDate("Time"));
                ds.add(TinNhan);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(TinNhan_DTO TinNhan)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String Time = fmt.format(TinNhan.getTime());
            String sql = "Insert TinNhan(TK_Gui, TK_Nhan, TinNhan, Active, Time) Values('"+TinNhan.getTK_Gui()+"','"+TinNhan.getTK_Nhan()+"','"+TinNhan.getTinNhan()+"','"+TinNhan.isActive()+"','"+Time+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(TinNhan_DTO TinNhan)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String Time = fmt.format(TinNhan.getTime());
            String sql = "Update TinNhan Set TK_Gui = '"+ TinNhan.getTK_Gui() +"', TK_Nhan = '"+TinNhan.getTK_Nhan()+"', TinNhan = '"+TinNhan.getTinNhan()+"', Time = '"+Time+ "' where ID = "+ TinNhan.getID();
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(int maID)
    {
        boolean flag = false;
        try {
            String sql = "Delete from TinNhan where ID = "+ maID;
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
}
